﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace ISS.IR.RRNMS.Library.DataAccess
{
    public static partial class Main
    {
        public static List<EntityField> GetEntityFields(int entityId)
        {
            try
            {
                command.CommandText = "sp_GetEntityFields";
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Clear();

                command.Parameters.Add(new SqlParameter("@entityId", entityId));

                DataTable dtMaster = new DataTable();

                adapter.Fill(dtMaster);

                if (dtMaster != null && dtMaster.Rows.Count > 0)
                {
                    List<EntityField> fields = new List<EntityField>();

                    foreach (DataRow drCurrentEntityFields in dtMaster.Rows)
                    {
                        EntityField field = new EntityField();

                        field.FieldId = Convert.ToInt32(drCurrentEntityFields["FieldId"]);

                        if (drCurrentEntityFields["FieldDisplayName"] != DBNull.Value)
                            field.FieldDisplayName = drCurrentEntityFields["FieldDisplayName"].ToString();

                        if (drCurrentEntityFields["FieldSchemaName"] != DBNull.Value)
                            field.FieldSchemaName = drCurrentEntityFields["FieldSchemaName"].ToString();

                        if (drCurrentEntityFields["EntityId"] != DBNull.Value)
                            field.Entity = new Item { Id = Convert.ToInt32(drCurrentEntityFields["EntityId"]), Value = drCurrentEntityFields["EntityName"].ToString() };

                        if (drCurrentEntityFields["TypeId"] != DBNull.Value)
                            field.Type = new Item { Id = Convert.ToInt32(drCurrentEntityFields["TypeId"]), Value = drCurrentEntityFields["TypeName"].ToString() };

                        if (drCurrentEntityFields["CategoryId"] != DBNull.Value)
                            field.Category = new Item { Id = Convert.ToInt32(drCurrentEntityFields["CategoryId"]), Value = drCurrentEntityFields["CategoryName"].ToString() };

                        fields.Add(field);
                    }

                    return fields;

                }

                else
                    return null;
            }
            catch (SqlException sqlExc)
            {
                return null;
            }
            catch (Exception exc)
            {
                return null;
            }
        }
    }
}
